﻿using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using MPACore.Phonebook.Authorization.Roles;
using MPACore.Phonebook.Authorization.Users;
using MPACore.Phonebook.MultiTenancy;
using MPACore.Phonebook.Phonebooks.Persons;
using MPACore.Phonebook.Phonebooks.PhoneNumbers;

namespace MPACore.Phonebook.EntityFrameworkCore
{
    public class PhonebookDbContext : AbpZeroDbContext<Tenant, Role, User, PhonebookDbContext>
    {
        /* Define a DbSet for each entity of the application */
        public DbSet<Person> Persons { get; set; }
        public DbSet<PhoneNumber> PhoneNumbers { get; set; }


        public PhonebookDbContext(DbContextOptions<PhonebookDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("PB");
            // 忽略姓氏
            modelBuilder.Entity<User>().Ignore(u => u.Surname);
            // 设置姓名和邮件地址可空
            modelBuilder.Entity<User>().Property(u => u.Name).IsRequired(false);
            modelBuilder.Entity<User>().Property(u => u.EmailAddress).IsRequired(false);
            modelBuilder.Entity<Person>().ToTable("Person", "PB");
            modelBuilder.Entity<PhoneNumber>().ToTable("PhoneNumnber", "PB");
        }
    }
}
